package library.view;

import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import library.dao.Impl.UserDaoImpl;
import library.pojo.User;

/**
 * @author zaizai
 *
 */
public class LoginFrame extends JFrame {
	private JTextField userNameTextField;
	private JTextField passwordTextField;
	private JComboBox typeComboBox;
	public static void main(String[] args) {
		new LoginFrame();
	}

	public LoginFrame() {
		ImageIcon icon = new ImageIcon(MainJTPane.class.getResource("/title.png"));
		this.setIconImage(icon.getImage());
		setBounds(100, 100, 685, 528);
		
		JPanel panel=(JPanel)this.getContentPane();
		panel.setOpaque(false);
		ImageIcon icon1 = new ImageIcon("res/2.png");
		JLabel label = new JLabel(icon1);
		label.setBounds(0, 0, this.getWidth(), this.getHeight());
		icon1.setImage(icon1.getImage().getScaledInstance(label.getWidth(), label.getHeight(), Image.SCALE_DEFAULT));
		this.getLayeredPane().add(label,Integer.valueOf(Integer.MIN_VALUE));
		
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
		this.setLayout(null);

		JLabel titleLabel = new JLabel("图书管理系统");
		titleLabel.setIcon(new ImageIcon("res/book.png"));
		titleLabel.setFont(new Font("宋体", Font.PLAIN, 40));
		titleLabel.setBounds(183, 31, 299, 57);
		this.add(titleLabel);

		JLabel userNameLabel = new JLabel("用户名：");
		userNameLabel.setIcon(new ImageIcon("res/user.png"));
		userNameLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		userNameLabel.setBounds(143, 215, 120, 30);
		this.add(userNameLabel);

		JLabel passwordLabel = new JLabel("密码：");
		passwordLabel.setIcon(new ImageIcon("res/password.png"));
		passwordLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		passwordLabel.setBounds(163, 284, 110, 30);
		this.add(passwordLabel);

		userNameTextField = new JTextField();
		userNameTextField.setBounds(278, 220, 214, 24);
		this.add(userNameTextField);
		userNameTextField.setColumns(10);

		passwordTextField = new JTextField();
		passwordTextField.setBounds(278, 289, 214, 24);
		this.add(passwordTextField);
		passwordTextField.setColumns(10);

		JButton loginButton = new JButton("登录");
		loginButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionActionPerformed(e);
			}
		});
		loginButton.setFont(new Font("宋体", Font.PLAIN, 17));
		loginButton.setBounds(193, 363, 94, 27);
		this.add(loginButton);

		JButton registerButton = new JButton("注册");
		registerButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetValueActionPerformed(e);
			}
		});
		registerButton.setFont(new Font("宋体", Font.PLAIN, 17));
		registerButton.setBounds(398, 363, 94, 27);
		this.add(registerButton);

		JLabel typeLabel = new JLabel("用户类型");
		typeLabel.setFont(new Font("宋体", Font.PLAIN, 23));
		typeLabel.setBounds(149, 128, 104, 32);
		this.add(typeLabel);

		String[] ways = { "借阅者", "图书管理员", "系统管理员" };
		typeComboBox = new JComboBox(ways);
		typeComboBox.setFont(new Font("宋体", Font.PLAIN, 15));
		typeComboBox.setBounds(278, 133, 214, 30);
		this.add(typeComboBox);

		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}

	/**
	 * 登录事件处理
	 */
	private void actionActionPerformed(ActionEvent evt) {
		UserDaoImpl userDao = new UserDaoImpl();
		String userName = this.userNameTextField.getText();
		String password = new String(this.passwordTextField.getText());
		if (this.isEmpty(userName)) {
			JOptionPane.showMessageDialog(null, "用户名不能为空！");
			return;
		}
		if (this.isEmpty(password)) {
			JOptionPane.showMessageDialog(null, "密码不能为空！");
			return;
		}
		if(!userName.matches("[0-9]{0,12}") && userName.length() == 12) {
			JOptionPane.showMessageDialog(null, "请输入正确的用户名！");
			return;
		}		
		userDao = new UserDaoImpl();
		User user = new User(userName, password, userDao.getUserName(userName), userDao.getType(userName),
				userDao.getPhone(userName));
		User currentUser = userDao.login(user);
		if (currentUser != null && typeComboBox.getSelectedItem().equals(userDao.getType(userName))) {
			this.dispose();
			if (typeComboBox.getSelectedItem().equals("借阅者")) {
				TotalFrameCommon t = new TotalFrameCommon(currentUser);
			}
			if (typeComboBox.getSelectedItem().equals("图书管理员")) {
				TotalFrameBook t = new TotalFrameBook(currentUser);
			}
			if (typeComboBox.getSelectedItem().equals("系统管理员")) {
				TotalFrameSystem t = new TotalFrameSystem(currentUser);
			}
		} else {
			JOptionPane.showMessageDialog(null, "用户名或密码错误！");
		}
	}

	/**
	 * 注册事件处理
	 */
	private void resetValueActionPerformed(ActionEvent evt) {
		RegisterDialog r = new RegisterDialog();
	}

	/**
	 * 判断是否为空
	 */
	private boolean isEmpty(String str) {
		if (str == null || "".equals(str.trim())) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 判断是否不为空
	 */
	private boolean isNotEmpty(String str) {
		if (str != null && !"".equals(str.trim())) {
			return true;
		} else {
			return false;
		}
	}
}
